﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Common;
using MyWeb.Data;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucUserManager : UserControl
    {
        private readonly DistributionService _distributionService = new DistributionService();
        private readonly StatisticsService _statisticsService = new StatisticsService();
        private readonly UserService _userService = new UserService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int insert = Int32.Parse(Request.QueryString["in"]);
                if (insert == 1)
                {
                    RefreshControl();
                    txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                }
                else
                {
                    LoadDefault();
                    int idUser = Int32.Parse(Request.QueryString["id"]);
                    DataTable dt = _userService.User_GetById(idUser);
                    txtUsername.Text = dt.Rows[0]["username"].ToString();
                    txtPassword.Text = dt.Rows[0]["password"].ToString();
                    txtRePassword.Text = dt.Rows[0]["password"].ToString();
                    txtName.Text = dt.Rows[0]["name"].ToString();
                    ddlGender.SelectedValue = dt.Rows[0]["gender"].ToString();
                    txtEmail.Text = dt.Rows[0]["email"].ToString();
                    txtBirthday.Text = DateTime.Parse(dt.Rows[0]["birthday"].ToString()).ToString("dd/MM/yyyy");
                    txtAddress.Text = dt.Rows[0]["address"].ToString();
                    txtPhone.Text = dt.Rows[0]["phone"].ToString();
                    ddlRule.SelectedValue = dt.Rows[0]["rule"].ToString();
                    if (Boolean.Parse(dt.Rows[0]["status"].ToString()))
                    {
                        ckKichhoat.Checked = true;
                    }
                    else
                    {
                        ckKichhoat.Checked = false;
                    }
                }
            }
        }

        private void RefreshControl()
        {
            txtName.Text = string.Empty;
            txtUsername.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtRePassword.Text = string.Empty;
        }

        private void LoadDefault()
        {
         txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }

        //protected void ckDoimk_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (ckDoimk.Checked)
        //    {
        //        txtPassword.ReadOnly = false;
        //        txtRePassword.ReadOnly = false;
        //    }
        //    else
        //    {
        //        txtPassword.ReadOnly = true;
        //        txtRePassword.ReadOnly = true;
        //    }
        //}

        protected void btnCancelAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/View.aspx?action=ucUserView");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool active = ckKichhoat.Checked;
            var userInfo = new UserInfo
                               {
                                   Name = txtName.Text,
                                   Username = txtUsername.Text,
                                   Password = StringClass.Encode("sha256", txtPassword.Text.Trim()),
                                   Rule = 0,
                                   Birthday = DateTime.Parse(txtBirthday.Text).ToString("yyyy-MM-dd"),
                                   Gender = Convert.ToBoolean(ddlGender.SelectedValue),
                                   Email = txtEmail.Text,
                                   Address = txtAddress.Text,
                                   Phone = txtPhone.Text,
                                   CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
                                   Status = active
                               };
            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                userInfo.Password = txtPassword.Text;
                _userService.User_Insert(userInfo);
                var statistics = new StatisticsInfo {User_Id = _userService.User_GetId()};
                DataTable dtDistribution = _distributionService.Distribution_GetByAll();
                for (int i = 0; i < dtDistribution.Rows.Count; i++)
                {
                    statistics.Distribution_Id = Int32.Parse(dtDistribution.Rows[i]["Id"].ToString());
                    _statisticsService.Statistic_Insert(statistics);
                }
            }
            //else
            //{
            //    if (ckDoimk.Checked)
            //    {
            //        userInfo.Password = txtPassword.Text;
            //        _userService.User_Update(userInfo);
            //    }
            //    else
            //    {
            //        _userService.User_UpdateNoPass(userInfo);
            //    }
            //}
            Response.Redirect("~/Admin/View.aspx?action=ucUserView");
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            bool active = ckKichhoat.Checked;
            var userInfo = new UserInfo
                               {
                                   Name = txtName.Text,
                                   Username = txtUsername.Text,
                                   Password = StringClass.Encode("sha256", txtPassword.Text.Trim()),
                                   Rule = 0,
                                   Birthday = DateTime.Parse(txtBirthday.Text).ToString("yyyy-MM-dd"),
                                   Gender = Convert.ToBoolean(ddlGender.SelectedValue),
                                   Email = txtEmail.Text,
                                   Address = txtAddress.Text,
                                   Phone = txtPhone.Text,
                                   CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
                                   Status = active
                               };
            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _userService.User_Insert(userInfo);
            }
            //else
            //{
            //    if (ckDoimk.Checked)
            //    {
            //        _userService.User_Update(userInfo);
            //    }
            //    else
            //    {
            //        _userService.User_UpdateNoPass(userInfo);
            //    }
            //}
        }
    }
}